<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品单价" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入商品单价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品类别" prop="goodsType">
        <el-input
          v-model="queryParams.goodsType"
          placeholder="请输入商品类别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
<el-dialog :title="title" :visible.sync="open" width="1500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
  <el-row>
    <el-col :span="16">
      <el-form-item  prop="goodsPicture">
          <template>
            <img
              v-if="form.goodsPicture"
              :span="16"
              width="100%"
              height="100%"
              object-fit="cover"
              :src="getImage(form.goodsPicture)"
              class="avatar"
            />
          </template>
      </el-form-item>
    </el-col>
    <el-col :span="8">
        <el-form-item label="名称" prop="goodsName">
          <el-input v-text="form.goodsName" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="简介" prop="goodsDesc">
          <el-input v-text="form.goodsDesc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="供货商" prop="supplierId">
          <el-input v-text="form.supplierId" placeholder="请输入供货商id" />
        </el-form-item>
        <el-form-item label="商品单价" prop="price">
          <el-input v-text="form.price" placeholder="请输入商品单价" />
        </el-form-item>
        <el-form-item label="库存" prop="supplierStock">
          <el-input v-text="form.goodsStock" placeholder="请输入供应商库存" />
        </el-form-item>
    </el-col>
  </el-row>
      </el-form>
      <el-table :data="comments" style="width: 100%">
        <el-table-column prop="comments" label="评论内容" />
        <el-table-column prop="userId" label="评论人" />
        <el-table-column prop="updateTime" label="评论时间" />
        <el-table-column prop="status" label="评论倾向">
          <template slot-scope="scope">
              <span >{{ scope.row.status=='1'?"👍":"👎" }} </span>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @current-change="getCommentList"
        :current-page="queryCommentParams.pageNum"
        :page-size="queryCommentParams.pageSize"
        :total="comTotal"
        layout="prev, pager, next"
      >
      </el-pagination>
  <div slot="footer" class="dialog-footer">
    <el-button @click="cancel">取 消</el-button>
  </div>
</el-dialog>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-table :data="goodsSearchList" @selection-change="handleSelectionChange" ref="tGoodsInorders">
                <el-table-column type="selection" width="55" align="center"/>
                <el-table-column label="序号" align="center" prop="goodsId"/>
                <el-table-column label="名称" align="center" prop="goodsName" />
                <el-table-column label="简介" align="center" prop="goodsDesc" />
                <el-table-column label="供货商" align="center" prop="supplierId" />
                <el-table-column label="商品类别" align="center" prop="goodsType" />
                <el-table-column label="商品单价" align="center" prop="price" />
                <el-table-column label="商品库存" align="center" prop="goodsStock" />
      <el-table-column label="商品图片" align="center" prop="goodsPicture" >
        <template slot-scope="{ row }">
          <img
            width="50px"
            height="50px"
            v-if="row.goodsPicture"
            :src="getImage(row.goodsPicture)"
            class="avatar"
          />
        </template>
      </el-table-column>

          <el-table-column label="商品订购个数" prop="amount" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.amount" placeholder="请输入商品订购个数" />
            </template>
          </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="handleUpdate(scope.row)"
                    >查看详情</el-button>
                  </template>
                </el-table-column>
        </el-table>
        <el-form-item label="订单备注" prop="orderDesc">
          <el-input v-model="form.orderDesc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="到货地址" prop="addr">
          <el-input v-model="form.addr" placeholder="请输入到货地址,留空则使用默认地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">创建订单</el-button>
      </div>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>



<script>
import { listGoodsManage, getGoodsManage, updateGoodsManage,getGoodInfoManage } from "@/api/supplier/goodsManage";
import { addOrderDetailInfo} from "@/api/buyers/orderDetailInfo";
import { listComments} from "@/api/buyers/comments";
import { getPurchaser} from "@/api/system/purchaser";

import { getSupplier } from "@/api/system/supplier";
export default {
  name: "GoodsSearch",
  data() {
    return {
      comTotal:0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: {},
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      checkedTGoodsInorders: [],
      // 总条数
      total: 0,
      // 商品查询表格数据
      goodsSearchList: [],
      // 商品查询表格数据
      goodsSelectionList: [],
      // 弹出层标题
      title: "",
      //选择商品信息
      tGoodsInordersList: [],
      comments:[],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsName: null,
        goodsDesc: null,
        goodsType: null,
        price: null,
        status: null,
      },
            // 查询参数
      queryCommentParams: {
         pageNum: 1,
         pageSize: 2,
         goodsId:null,
      },
      prices:"0",
      selected:null,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        supplierId: [
          { required: true, message: "供货商id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
      reset() {
        this.form = {
          orderId: null,
          orderNo: null,
          orderDesc: null,
          purchaseId: null,
          addr: null,
          createTime: null,
          updateTime: null,
          updateBy: null,
          status: '1'
        };
        this.resetForm("form");
      },
      getsupplierName(supplierId,i){
        getSupplier(supplierId).then(
          resp=>{
            this.goodsSearchList[i].supplierId=resp.data.supplierName;
          }
        )
      },
    /** 查询商品查询列表 */
    getList() {
      this.loading = true;
      this.queryParams.status = "1";
      console.log(this.queryParams)
      listGoodsManage(this.queryParams).then(response => {
        this.goodsSearchList = response.rows;
        for(var i = 0; i < response.rows.length; i++) {
          this.goodsSearchList[i].selection=false;
          this.getsupplierName(response.rows[i].supplierId,i);
        }
        this.total = response.total;
        this.loading = false;
      });
    },
        getCommentList(currentPage) {
          this.queryCommentParams.pageNum=currentPage;
          listComments(this.queryCommentParams).then(async response => {
            for(var i = 0; i < response.rows.length; i++) {
              var result = await getPurchaser(response.rows[i].userId)
              console.log(result)
              response.rows[i].userId=result.data.purchaserName;
            }
            this.comments = response.rows;
            this.comTotal = response.total;
          });
        },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {

        let ids=this.goodsSearchList.filter(function(item) {
                return item.selection == true;
              });
        ids=ids.map(item=>{return {"goodsId":item.goodsId,"amount":item.amount,"orderId":""}})
        if (valid&&ids.length!=0) {
          for(var i=0; i<ids.length; i++){
            var str = ids[i].amount;
            if(this.goodsSearchList[i].amount==null||this.goodsSearchList[i].amount==''){
              ids[i].amount='1';
              continue;
            }
            if(!isFinite(str)){
              this.$modal.msgSuccess("非数字");
              return;
            }
            if(str.toString().indexOf(".") != -1){
              this.$modal.msgSuccess("非整数");
              return;
            }
            var num=parseInt(str);
            if(num <= 0){
              this.$modal.msgSuccess("小于0");
              return;
            }
          }
          addOrderDetailInfo(this.form,ids);
          this.$modal.msgSuccess("创建成功");
        }
        else this.$modal.msgSuccess("无选中商品");
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.goodsId)
      for(var i=0;i< this.goodsSearchList.length;i++) {
        if(this.ids.includes(this.goodsSearchList[i].goodsId)) {
          this.goodsSearchList[i].selection=true;
          }
        else {this.goodsSearchList[i].selection=false;}
      }
    },
    /** 查看详情按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.queryCommentParams.goodsId = row.goodsId;
      const goodsId = row.goodsId || this.ids
      getGoodsManage(goodsId).then(async response => {
        var result = await getSupplier(response.data.supplierId)
        response.data.userId=result.data.supplierName;
        response.data.goodsStock=row.goodsStock;
        this.form = response.data;
        this.form.supplierId = row.supplierId;
        this.open = true;
        this.title = "商品详情";
      });
      this.getCommentList();
    },
        getImage (image) {
          return `http://localhost:8080/system/common/download?name=${image}`
        }
  }
};
</script>
